import dva from 'dva';
import { createBrowserHistory } from 'history';
import aopc from 'aopc';
import models from './models/index';
import './index.scss';

const getModels = () => {
    const list = aopc.getModels();
    const obj = {};

    list.forEach(m => {
        if (obj[m.namespace]) {
            const m_obj = obj[m.namespace];
            const { state, effects, reducers, actions } = m;
            m_obj.state = { ...m_obj.state, ...state };
            m_obj.effects = { ...m_obj.effects, ...effects };
            m_obj.reducers = { ...m_obj.reducers, ...reducers };

            const actions2 = {};
            m_obj.actions.concat(actions).forEach(a => {
                if (!actions2[a.actionName]) actions2[a.actionName] = a;
            });
            m_obj.actions = [];
            for (const key in actions2) {
                m_obj.actions.push(actions2[key]);
            }
        }
        else obj[m.namespace] = m;
    });

    return obj;
};

// 1. Initialize
const app = dva({ history: createBrowserHistory() });

// 2. Plugins
// app.use({});

// 3. Model
models.forEach(m => app.model(m));
const modelObj = getModels();
for (const key in modelObj) {
    app.model(modelObj[key]);
}

// 4. Router
app.router(require('./router').default);

// 5. Start
app.start('#root');

document.body.addEventListener('touchstart', () => { }, false);